<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 思路：因为判断是否有重叠，必定是数组的尾元素和另一个数组头元素比较大小，判断是否有重叠，
                因此可以对数组第二个元素进行排序
            */
            //[[1,2],[2,3],[3,4],[1,3]]
            var eraseOverlapIntervals = function (intervals) {
                intervals.sort((a, b) => a[1] - b[1])
                let temp = intervals[0]
                let result = 0
                for (let i = 1; i < intervals.length; i++) {
                    if (temp[1] > intervals[i][0]) {
                        result++
                    } else {
                        //如果没有重叠，temp等于下一个
                        temp = intervals[i]
                    }
                }
                return result
            }
        </script>
    </body>
</html>
